﻿using EnterpriseDT.Net.Ftp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FTP_Client
{



    //1.加载时判断传入值是否为空，为空则不刷新，不为空则根据参数获得FTP文件夹信息
    public partial class FTPForm : Form
    {
        private const string ftpServerIP = "172.168.10.180";
        private const string ftpRemotePath = "";
        private const string ftpUserID = "project";
        private const string ftpPassword = "123456";
        private string PID = "";

        //public FTPForm()
        //{
        //    InitializeComponent();
        //}
        public FTPForm(string ProjectID)
        {
            InitializeComponent();
            //程序加载时更新列表

            PID = ProjectID;

            GetList(ProjectID);
            
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UploadFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog opd = new OpenFileDialog();
            if (opd.ShowDialog() == DialogResult.OK)
            {
                Connection(PID,opd.FileName, opd.SafeFileName, true, false);
            }

        }
        /// <summary>
        /// 获得FTP目录
        /// </summary>
        public void GetList(string PID = "false")
        {
            this.listView1.Items.Clear();

            using (var conn = new FTPConnection
            {
                ServerAddress = ftpServerIP,
                ServerDirectory = ftpRemotePath,
                UserName = ftpUserID,
                Password = ftpPassword,
                CommandEncoding = Encoding.GetEncoding("GBK")
            })
            {
                conn.Connect();


                //判断是否有此子文件夹，若无则创建
                if (!conn.DirectoryExists(@"\"+ PID))
                {
                    conn.CreateDirectory(@"\"+ PID);
                }
                //"+PID+"
                //============切换当前工作目录到L11============  
                conn.ChangeWorkingDirectory(@""+ PID);

                //获得文件列表
                var files1 = conn.GetFileInfos();
                this.listView1.BeginUpdate();
                DateTime dt = new DateTime();
                string filedanwei = "字节";
                foreach (var item in files1)
                {
                    
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = item.Name;

                    if (item.Size < 1024)
                    {
                        filedanwei = "字节";
                    }
                    else
                    {
                        item.Size = item.Size / 1024;
                        filedanwei = "kb";
                    }


                    lvi.SubItems.Add(item.Size.ToString() + filedanwei);

                    //---------------------------------------------------------------------------
                    //对非正常时间进行处理
                    //---------------------------------------------------------------------------
                    dt = Convert.ToDateTime(item.LastModified).AddHours(8);
                    lvi.SubItems.Add(dt.ToString());
                    //---------------------------------------------------------------------------
                    //---------------------------------------------------------------------------
                    //lvi.SubItems.Add(item.LastModified.ToString());
                    lvi.Tag = item.Path;
                    this.listView1.Items.Add(lvi);
                }
                this.listView1.EndUpdate();
            }
        }

        private void DownloadFile_Click(object sender, EventArgs e)
        {
            string remotepath = this.listView1.SelectedItems[0].Tag.ToString();
            string localfilename = this.listView1.SelectedItems[0].Text.ToString();

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            sfd.FilterIndex = 2;
            sfd.RestoreDirectory = true;
            sfd.FileName = localfilename;
            //sfd.Filter = "All files (*.*)";
            //sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Connection(PID,sfd.FileName, remotepath, false, true);
            }
        }

        //封装连接方法

            /// <summary>
            /// 连接FTP方法
            /// </summary>
            /// <param name="LocalPath">当上传时为本地文件地址，当下载时为保存本地文件名字</param>
            /// <param name="RemoteFile">当上传时为FTP文件地址，当下载时为FTP文件路径</param>
            /// <param name="UploadFile">布尔判断上传方法</param>
            /// <param name="DownloadFile">布尔判断下载方法</param>
        public void Connection(string PID,string LocalPath,string RemoteFile,bool UploadFile = false, bool DownloadFile=false)
        {
            using (var conn = new FTPConnection
            {
                ServerAddress = ftpServerIP,
                ServerDirectory = ftpRemotePath,
                UserName = ftpUserID,
                Password = ftpPassword,
                CommandEncoding = Encoding.GetEncoding("GBK")
            })
            {
                conn.Connect();

                if (UploadFile)
                {
                    conn.ChangeWorkingDirectory(@"\"+ PID);
                    conn.UploadFile(LocalPath, RemoteFile);
                }
                if (DownloadFile)
                {
                    conn.DownloadFile(LocalPath, RemoteFile);
                }
                GetList();
            }
        }

    }
}